<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!-- <el-form-item label="类型" prop="giftType">
        <el-select v-model="queryParams.giftType" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in giftTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-radio-group v-model="queryParams.giftType" @change="handleQuery">
          <el-radio-button v-for="dict in giftTypeOptions" :key="dict.dictValue" :label="dict.dictValue">{{dict.dictLabel}}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item> -->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:gift:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="giftList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column label="类型" align="center" prop="giftType" :formatter="giftTypeFormat" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="图片" align="center" prop="giftImg">
        <template slot-scope="scope">
          <el-image v-if="scope.row.giftImg" style="width: 50px; height: 50px" 
          :src="scope.row.giftImg" :preview-src-list="[scope.row.giftImg]" />
        </template>
      </el-table-column>
      <el-table-column label="兑换值" align="center" prop="foodNumber" />
      <el-table-column label="礼物位置" align="center" prop="position" :formatter="positionFormat" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:gift:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:gift:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改礼品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="礼物类型">
          <el-radio-group v-model="form.giftType">
            <el-radio v-for="dict in giftTypeOptions" :key="dict.dictValue" :label="dict.dictValue">{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="礼物名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="猫粮值" prop="foodNumber">
          <el-input-number v-model="form.foodNumber" controls-position=right />
        </el-form-item>
        <el-form-item label="礼物图表" prop="giftImg">
          <SingleUpload :avatar="form.giftImg" @emitSetUrl="setUrl" />
        </el-form-item>
        <el-form-item label="礼物位置">
          <el-select v-model="form.position" >
            <el-option v-for="dict in positionOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="parseInt(dict.dictValue)" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.giftType==='1'" label="等级/身份" prop="level">
          <el-select v-model="form.level">
            <el-option v-for="(level,index) in levelOption" :key="index" :label="level.name" :value="level.id" />
          </el-select>
          或
          <el-select v-model="form.noble">
            <el-option v-for="(noble,index) in nobleOption" :key="index" :label="noble.name" :value="noble.id" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.giftType==='1'" label="经验加成" prop="experience">
          <el-input-number v-model="form.experience" :precision="2" :step="0.1" placeholder="%"  controls-position=right />
        </el-form-item>
        <el-form-item v-if="form.giftType===3" label="间隔时长" prop="intervalTime">
          <el-input-number v-model="form.intervalTime" placeholder="分钟"  controls-position=right />
        </el-form-item>
        <el-form-item label="上架/下架">
          <el-switch v-model="form.status" active-color="#ff4949" :active-value="0" :inactive-value="1" inactive-color="#13ce66" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGift, getGift, delGift, addGift, updateGift, exportGift } from "@/api/system/gift";
import { loadUserLevel } from "@/api/system/userLevel";
import { loadUserNoble } from "@/api/system/userNoble";
import SingleUpload from '@/components/Upload/SingleUpload'
export default {
  name: "Gift",
  components:{SingleUpload},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 礼品表格数据
      giftList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型字典
      giftTypeOptions: [],
      // 礼物位置字典
      positionOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        giftType: 0,
        name: undefined,
        giftImg: undefined,
        foodNumber: undefined,
        position: undefined,
        level: undefined,
        experience: undefined,
        intervalTime: undefined,
        sort: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        giftType: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        giftImg: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        foodNumber: [
          { required: true, message: "兑换值不能为空", trigger: "blur" }
        ],
        position: [
          { required: true, message: "礼物位置不能为空", trigger: "blur" }
        ],
        level: [
          { required: true, message: "等级/身份不能为空", trigger: "blur" }
        ],
        experience: [
          { required: true, message: "经验加成不能为空", trigger: "blur" }
        ],
        intervalTime: [
          { required: true, message: "间隔时长(分)不能为空", trigger: "blur" }
        ],
      },
      levelOption:[],
      nobleOption:[],
    };
  },
  created() {
    this.getList();
    this.getDicts("gift_type").then(response => {
      this.giftTypeOptions = response.data;
    });
    this.getDicts("gift_position").then(response => {
      this.positionOptions = response.data;
    });
    loadUserLevel().then(response => {
      this.levelOption = response.data;
    });
    loadUserNoble().then(response => {
      this.nobleOption = response.data;
    });
  },
  methods: {
    setUrl(data){
      this.form.giftImg = data.url
    },
    /** 查询礼品列表 */
    getList() {
      this.loading = true;
      listGift(this.queryParams).then(response => {
        this.giftList = response.rows;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    // 类型字典翻译
    giftTypeFormat(row, column) {
      return this.selectDictLabel(this.giftTypeOptions, row.giftType);
    },
    // 礼物位置字典翻译
    positionFormat(row, column) {
      return this.selectDictLabel(this.positionOptions, row.position);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        giftType: "0",
        name: undefined,
        giftImg: undefined,
        foodNumber: undefined,
        position: "0",
        level: undefined,
        experience: undefined,
        intervalTime: undefined,
        sort: undefined,
        createTime: undefined,
        createBy: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加礼品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGift(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改礼品";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateGift(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addGift(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除礼品编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGift(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有礼品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportGift(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>